package com.mingloucct.payment_ws.controller;

import com.mingloucct.payment_ws.entity.Neishen;
import com.mingloucct.payment_ws.entity.PageBean;
import com.mingloucct.payment_ws.entity.Photo;
import com.mingloucct.payment_ws.entity.ShenHeInfo;
import com.mingloucct.payment_ws.service.INeishenService;
import com.mingloucct.payment_ws.service.IPhotoService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping(value = "/neishen")
public class NeishenController {
    @Autowired
    private INeishenService iNeishenService;

    @Autowired
    private IPhotoService iPhotoService;

    @RequestMapping(value = "/readNeishen",method = RequestMethod.GET)
    public void query(HttpServletResponse resp,@RequestParam(required = false,defaultValue = "%") String quary,@RequestParam(required = false ,defaultValue = "1") int PageNum,@RequestParam(required = false ,defaultValue = "10") int PageSize) {
        List<Neishen> neishen = iNeishenService.readNeishen(quary);
        PageBean<Neishen> pb = new PageBean<Neishen>();
        int pc = PageNum;//当前页码
        int ps = PageSize;  //每页记录数
        int total = neishen.size();//集合的长度就是行数（记录总数）
        pb.setTr(total);//分页封装类，添加属性记录总数
        pb.setPs(ps);//添加属性每页记录数
        pb.setPc(pc);//添加属性当前页码
        List NowList = new ArrayList();//当前list
        for(int i = (pc-1)*ps;i<pc*ps;i++){
            //每行ps个记录数，i最大只能循环ps次，当i+1大于数组的长度时，说明数组已经到头
            if(i<neishen.size()){
                NowList.add(neishen.get(i));//你要执行的内容
            }else break;
        }
        pb.setBeanList(NowList);//将获取到的该页数据放入pb中
        try {
                     /*list集合中存放的是好多student对象*/
                         /*将list集合装换成json对象*/
                        JSONArray data = JSONArray.fromObject(NowList);
                         //接下来发送数据
                        /*设置编码，防止出现乱码问题*/
                     JSONObject data1 = new JSONObject();
                     data1.put("Custom",NowList);
                     data1.put("PageNum",pb.getPc());
                     data1.put("PageSize",pb.getPs());
                     data1.put("total",total);
                         resp.setCharacterEncoding("utf-8");
                         /*得到输出流*/
                         PrintWriter respWritter = resp.getWriter();
                         /*将JSON格式的对象toString()后发送*/
                         respWritter.append(data1.toString());
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
           }
    @RequestMapping(value = "/readWaishen",method = RequestMethod.GET)
    public void query1(HttpServletResponse resp,@RequestParam(required = false,defaultValue = "%") String quary,@RequestParam(required = false ,defaultValue = "1") int PageNum,@RequestParam(required = false ,defaultValue = "10") int PageSize) {
        List<Neishen> waishen = iNeishenService.readWaishen(quary);
        PageBean<Neishen> pb = new PageBean<Neishen>();
        int pc = PageNum;//当前页码
        int ps = PageSize;  //每页记录数
        int total = waishen.size();//集合的长度就是行数（记录总数）
        pb.setTr(total);//分页封装类，添加属性记录总数
        pb.setPs(ps);//添加属性每页记录数
        pb.setPc(pc);//添加属性当前页码
        List NowList = new ArrayList();//当前list
        for(int i = (pc-1)*ps;i<pc*ps;i++){
            //每行ps个记录数，i最大只能循环ps次，当i+1大于数组的长度时，说明数组已经到头
            if(i<waishen.size()){
                NowList.add(waishen.get(i));//你要执行的内容
            }else break;
        }
        pb.setBeanList(NowList);//将获取到的该页数据放入pb中
        try {
            /*list集合中存放的是好多student对象*/
            /*将list集合装换成json对象*/
            JSONArray data = JSONArray.fromObject(NowList);
            //接下来发送数据
            /*设置编码，防止出现乱码问题*/
            JSONObject data1 = new JSONObject();
            data1.put("Custom",NowList);
            data1.put("PageNum",pb.getPc());
            data1.put("PageSize",pb.getPs());
            data1.put("total",total);
            resp.setCharacterEncoding("utf-8");
            /*得到输出流*/
            PrintWriter respWritter = resp.getWriter();
            /*将JSON格式的对象toString()后发送*/
            respWritter.append(data1.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/readFangkuan",method = RequestMethod.GET)
    public void query2(HttpServletResponse resp,@RequestParam(required = false,defaultValue = "%") String quary,@RequestParam(required = false ,defaultValue = "1") int PageNum,@RequestParam(required = false ,defaultValue = "10") int PageSize) {
        List<Neishen> fangkuan = iNeishenService.readFangkuan(quary);
        PageBean<Neishen> pb = new PageBean<Neishen>();
        int pc = PageNum;//当前页码
        int ps = PageSize;  //每页记录数
        int total = fangkuan.size();//集合的长度就是行数（记录总数）
        pb.setTr(total);//分页封装类，添加属性记录总数
        pb.setPs(ps);//添加属性每页记录数
        pb.setPc(pc);//添加属性当前页码
        List NowList = new ArrayList();//当前list
        for(int i = (pc-1)*ps;i<pc*ps;i++){
            //每行ps个记录数，i最大只能循环ps次，当i+1大于数组的长度时，说明数组已经到头
            if(i<fangkuan.size()){
                NowList.add(fangkuan.get(i));//你要执行的内容
            }else break;
        }
        pb.setBeanList(NowList);//将获取到的该页数据放入pb中
        try {
            /*list集合中存放的是好多student对象*/
            /*将list集合装换成json对象*/
            JSONArray data = JSONArray.fromObject(NowList);
            //接下来发送数据
            /*设置编码，防止出现乱码问题*/
            JSONObject data1 = new JSONObject();
            data1.put("Custom",NowList);
            data1.put("PageNum",pb.getPc());
            data1.put("PageSize",pb.getPs());
            data1.put("total",total);
            resp.setCharacterEncoding("utf-8");
            /*得到输出流*/
            PrintWriter respWritter = resp.getWriter();
            /*将JSON格式的对象toString()后发送*/
            respWritter.append(data1.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/updateByID",method = RequestMethod.GET)
    public void updateByID(@RequestParam String customID,
                           @RequestParam String date,
                           @RequestParam String CustomName,
                           @RequestParam String Province,
                           @RequestParam String City,
                           @RequestParam String Address,
                           @RequestParam String PostNum){
        iNeishenService.updateByID(customID,date,CustomName,Province,City,Address,PostNum);
    }
    @RequestMapping(value = "/updateShenheByID",method = RequestMethod.GET)
    public void updateShenheByID(@RequestParam("CustomID") String customID,
                                 @RequestParam("CarPrice") String CarPrice,
                                 @RequestParam("Prepay") String Prepay,
                                 @RequestParam("Dai_kuan_jin_e") String Dai_kuan_jin_e,
                                 @RequestParam("Zong_he_li_lv") String Zong_he_li_lv,
                                 @RequestParam("Dai_kuan_nian_shu") String Dai_kuan_nian_shu,
                                 @RequestParam("Yue_gong") String Yue_gong,
                                 @RequestParam("Dai_kuan_lei_xing") String Dai_kuan_lei_xing,
                                 @RequestParam("Marriage") String Marriage
    ){
        iNeishenService.updateShenheByID(customID,CarPrice,Prepay,Dai_kuan_jin_e,Zong_he_li_lv,Dai_kuan_nian_shu,Yue_gong,Dai_kuan_lei_xing,Marriage);
    }
    @RequestMapping(value = "/deleteByID",method = RequestMethod.GET)
    public void updateByID(@RequestParam String customID){
        iNeishenService.deleteByID(customID);
    }

    @RequestMapping(value = "/changeStatus",method = RequestMethod.GET)
    public void changeStatus(@RequestParam String customID){
        iNeishenService.changeStatus(customID);
    }

    @RequestMapping(value = "/changeFangkuan",method = RequestMethod.GET)
    public void changeByID(@RequestParam String customID,@RequestParam("amount") String amount){
        iNeishenService.changeByID(customID,amount);
    }
    private String filePath="C:\\Softwares\\Nginx\\nginx-1.18.0\\html\\wu_project\\images\\";
    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public String upLoadFile(@RequestParam("file") MultipartFile upload,@RequestParam("CustomID")String CustomID,@RequestParam("Type")int Type){
        //判断文件夹是否存在,不存在则创建
        File file=new File(filePath);
        if(!file.exists()){
            file.mkdirs();
        }
        String filepath=null;
        if(Type==1) filepath="贷款人身份证正面";
        if(Type==2) filepath="担保人身份证正面";
        if(Type==3) filepath="合同照";
        if(Type==4) filepath="贷款人身份证反面（人像面）";
        if(Type==5) filepath="担保人身份证反面";
        if(Type==6) filepath="房产证照片";
        String originalFileName = upload.getOriginalFilename();//获取原始图片的扩展名
        String newFileName = UUID.randomUUID()+originalFileName;
        String newFilePath=filePath+newFileName; //新文件的路径
        File file1=new File(newFilePath);
        if(!file1.exists()){
            file1.mkdirs();
        }
        try {
            upload.transferTo(new File(newFilePath));  //将传来的文件写入新建的文件
            String filename = newFileName;
            String url = "http://106.15.89.218:89/images/"+newFileName;
            String type = filepath;
            iNeishenService.uploadToDB(CustomID,url,type,filename);
            return newFilePath;
        }catch (IllegalStateException e ) {
            //处理异常
        }catch(IOException e1){
            //处理异常
        }
        return newFilePath;
    }

    @RequestMapping(value = "/selectShenHeInfo",method = RequestMethod.GET)
    public ShenHeInfo selectShenHeInfo(@RequestParam String customID){
       return iNeishenService.selectShenHeInfo(customID);
    }

    @RequestMapping(value = "/selectPhotoInfoByCid",method = RequestMethod.GET)
    public List<Photo> selectPhotoInfoByCid(@RequestParam String customID){
        return iPhotoService.selectPhotoInfoByCid(customID);
    }
}
